package com.kg.sdk.util;

import android.content.ClipboardManager;
import android.content.Context;
import android.widget.Toast;

import com.common.network.NetworkResponse;
import com.kg.sdk.network.KPHeaderSetting;

public class CommonUtil {

    public static void showToast(Context context,String text){
        Toast.makeText(context,text,Toast.LENGTH_LONG).show();
    }

    /**
     * unicode转字符串
     *
     * @param unicode
     * @return
     */
    public static String unicodeToString(String unicode) {
        StringBuffer sb = new StringBuffer();
        String[] hex = unicode.split("\\\\u");
        for (int i = 1; i < hex.length; i++) {
            int index = Integer.parseInt(hex[i], 16);
            sb.append((char) index);
        }
        return sb.toString();
    }

    public static String getRandomString(int stringLength) {
        String string = "abcdefghijklmnopqrstuvwxyz";
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < stringLength; i++) {
            int index = (int) Math.floor(Math.random() * string.length());//向下取整0-25
            sb.append(string.charAt(index));
        }
        return sb.toString();
    }

    public static void getXSID(NetworkResponse result){
        String x_sid = result.getString(KPNetworkData.X_SID_KEY);
        if(x_sid != null){
            KPHeaderSetting.x_sid = x_sid;
        }
    }


    public static void copy(Context context,String text){
        ClipboardManager cm = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
        // 将文本内容放到系统剪贴板里。
        cm.setText(text);
    }
}
